# -*- coding:utf-8 -*-

from app import db
from app.tools.timestamp import timestamp


class ModelMixin(object):
    def save(self):
        db.session.add(self)
        db.session.commit()


class Message(db.Model, ModelMixin):  # 后台消息
    __tablename__ = 'message'
    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.String(256), nullable=False)  # 消息实体
    timestamp = db.Column(db.String(40), index=True, default=timestamp)  # 时间戳
    type = db.Column(db.String(45))  # 消息类型  贷款/审核/分单

    def __init__(self, content, type):
        self.content = content
        self.type = type


class MessageMiddle(db.Model, ModelMixin):
    __tablename__ = 'message_middle'
    id = db.Column(db.Integer, primary_key=True)
    message_id = db.Column(db.Integer)  # 消息ID
    role = db.Column(db.String(45))  # 部门/角色
    server_id = db.Column(db.String(45))  # 客服ID
    status = db.Column(db.Boolean, default=False)  # 标记是否已读，True 是读了
    type = db.Column(db.String(45))  # 消息类型  贷款/审核/分单

    def read(self):
        self.status = True


class FrontMessage(db.Model, ModelMixin):  # 前台消息
    __tablename__ = 'front_message'
    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.String(256), nullable=False)  # 消息实体
    user_id = db.Column(db.String(30))  # 外部用户ID
    time = db.Column(db.DateTime)  # 时间
    message_type = db.Column(db.String(45))  # 消息类型
    status = db.Column(db.Boolean)    # 是否已读
